<template>
	<view class="activity-page">
		<!-- 下拉刷新容器 -->
		<scroll-view scroll-y class="scroll-container" :refresher-enabled="true" :refresher-triggered="refreshing"
			@refresherrefresh="onRefresh" @refresherrestore="onRestore">
			
			<!-- 骨架屏 -->
			<view class="skeleton-container" v-if="loading && activities.length === 0">
				<view class="skeleton-card" v-for="i in 2" :key="i">
					<view class="skeleton-image skeleton-animate"></view>
					<view class="skeleton-info">
						<view class="skeleton-title skeleton-animate"></view>
						<view class="skeleton-detail skeleton-animate"></view>
						<view class="skeleton-detail skeleton-animate"></view>
					</view>
				</view>
			</view>

			<!-- 活动列表 -->
			<view class="activity-list" v-else-if="activities.length > 0">
				<view class="activity-card" v-for="activity in activities" :key="activity.activityId"
					@click="handleActivityClick(activity.activityId)">
					<!-- 活动封面 -->
					<image :src="activity.image" mode="aspectFill" class="activity-image" lazy-load></image>

					<!-- 活动信息 -->
					<view class="activity-info">
						<!-- 活动标题 -->
						<text class="activity-title">{{ activity.title }}</text>

						<!-- 活动详情 -->
						<view class="activity-details">
							<view class="detail-item">
								<up-icon name="clock" size="14" color="#999999"></up-icon>
								<text class="detail-text">{{ activity.startTime }}</text>
							</view>
							<view class="detail-item">
								<up-icon name="map" size="14" color="#999999"></up-icon>
								<text class="detail-text">{{ activity.location }}</text>
							</view>
							<view class="detail-item">
								<up-icon name="account" size="14" color="#999999"></up-icon>
								<text class="detail-text">{{ activity.currentParticipants }}人报名</text>
							</view>
						</view>

						<!-- 活动状态 -->
						<view class="activity-status">
							<up-tag :text="getStatusText(activity.status)" :type="getStatusType(activity.status)" plain
								size="mini"></up-tag>
						</view>
					</view>
				</view>
			</view>

			<!-- 空状态 -->
			<view class="empty-view" v-else-if="!loading && activities.length === 0">
				<up-icon name="calendar" size="80" color="#cccccc"></up-icon>
				<text class="empty-text">暂无活动</text>
				<text class="empty-hint">期待精彩活动～</text>
			</view>
		</scroll-view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue';
	import {
		mockGetActivities
	} from '../../server/mock.js';
	import showToast from '../../utils/showToast.js';

	// 状态
	const loading = ref(true);
	const refreshing = ref(false); // 下拉刷新状态
	const activities = ref([]);

	/**
	 * 加载活动列表
	 */
	const loadActivities = async () => {
		loading.value = true;

		try {
			const res = await mockGetActivities({
				pageNum: 1,
				pageSize: 20
			});

			if (res.code === 200) {
				activities.value = res.data || [];
			} else {
				showToast(res.message || '加载失败', 'error');
			}
		} catch (error) {
			console.error('加载活动失败:', error);
			showToast('网络错误', 'error');
		} finally {
			loading.value = false;
		}
	};

	/**
	 * 获取状态文本
	 */
	const getStatusText = (status) => {
		const statusMap = {
			0: '未开始',
			1: '进行中',
			2: '已结束',
			3: '已取消'
		};
		return statusMap[status] || '未知';
	};

	/**
	 * 获取状态类型
	 */
	const getStatusType = (status) => {
		const typeMap = {
			0: 'primary',
			1: 'success',
			2: 'info',
			3: 'error'
		};
		return typeMap[status] || 'default';
	};

	/**
	 * 点击活动
	 */
	const handleActivityClick = (activityId) => {
		uni.navigateTo({
			url: `/pages/activity/activityDetail?activityId=${activityId}`
		});
	};

	/**
	 * 下拉刷新
	 */
	const onRefresh = async () => {
		refreshing.value = true;

		try {
			await loadActivities();
		} finally {
			// 延迟关闭刷新状态
			setTimeout(() => {
				refreshing.value = false;
			}, 300);
		}
	};

	/**
	 * 刷新恢复
	 */
	const onRestore = () => {
		refreshing.value = false;
	};

	// 页面加载
	onMounted(() => {
		loadActivities();
	});
</script>

<style lang="scss" scoped>
	.activity-page {
		min-height: 100vh;
		background-color: #f5f5f5;

		.scroll-container {
			height: 100vh;
			padding: 20rpx;
		}
	}

	// 骨架屏
	.skeleton-container {
		.skeleton-card {
			background-color: #ffffff;
			border-radius: 16rpx;
			overflow: hidden;
			margin-bottom: 20rpx;

			.skeleton-image {
				width: 100%;
				height: 350rpx;
				background-color: #f0f0f0;
			}

			.skeleton-info {
				padding: 30rpx;

				.skeleton-title {
					height: 40rpx;
					background-color: #f0f0f0;
					border-radius: 4rpx;
					margin-bottom: 20rpx;
					width: 80%;
				}

				.skeleton-detail {
					height: 24rpx;
					background-color: #f0f0f0;
					border-radius: 4rpx;
					margin-bottom: 12rpx;
					width: 60%;
				}
			}

			.skeleton-animate {
				animation: skeleton-loading 1.5s ease-in-out infinite;
				background: linear-gradient(90deg,
						#f0f0f0 25%,
						#e0e0e0 50%,
						#f0f0f0 75%);
				background-size: 200% 100%;
			}
		}
	}

	// 活动列表
	.activity-list {
		display: flex;
		flex-direction: column;
		gap: 20rpx;

		.activity-card {
			background-color: #ffffff;
			border-radius: 16rpx;
			overflow: hidden;
			box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.06);

			.activity-image {
				width: 100%;
				height: 350rpx;
				object-fit: cover;
			}

			.activity-info {
				padding: 30rpx;

				.activity-title {
					display: block;
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 20rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}

				.activity-details {
					display: flex;
					flex-direction: column;
					gap: 12rpx;
					margin-bottom: 20rpx;

					.detail-item {
						display: flex;
						align-items: center;
						gap: 10rpx;

						.detail-text {
							font-size: 24rpx;
							color: #999999;
						}
					}
				}

				.activity-status {
					display: flex;
					justify-content: flex-end;
				}
			}
		}
	}

	// 空状态
	.empty-view {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 200rpx 0;
		gap: 20rpx;

		.empty-text {
			font-size: 28rpx;
			color: #999999;
		}

		.empty-hint {
			font-size: 24rpx;
			color: #cccccc;
		}
	}

	// 骨架屏动画
	@keyframes skeleton-loading {
		0% {
			background-position: 200% 0;
		}

		100% {
			background-position: -200% 0;
		}
	}
</style>

